#include "PlayerShip.h"
void PlayerShip::Init()
{
	touchID = 0;
	position.x = 0.0;
	position.y = 0.0;
	ShipInitialValue* siv = GameObjectFactory::GetShipInitialValueFromName("PlayerDefault");
	if(siv)
	{
		this->SetSprite(SpriteManager::GetInstance()->LoadSpriteFromXMLFile(siv->spriteName));
		this->hp = siv->hp;
		this->damage = siv->damage;
	}
	//anim = spriteEngine->CreateAnimInstance(0);
	currentBullet = new Bullet();
	coolDownTime = 0.0f;
	material = new Material("Tex1_Diffuse");
}
void PlayerShip::Render()
{
	//spriteEngine->DrawAnim(anim, position.x + 48, position.y - 103);
	DXDevice::GetInstance()->SetActiveMaterial(material);
	sprite->DrawFrame(0,position.x,position.y);
}
void PlayerShip::Update(float timeDelta)
{
	//anim->Update(timeDelta);
	position = position + move*timeDelta*200.0;
	//move = Vector2f(0.0,0.0);
	coolDownTime += timeDelta;
	if(CanFire())
	{
		Fire();
		coolDownTime = 0;
	}
}
bool PlayerShip::UpdateInput()
{
	Touch touch = TouchManager::GetInstance()->GetTouch(0);
	if(touch.state == Touch::TouchState::Pressed)
	{
		touchID = touch.id;
		touchX = touch.x;
		touchY = touch.y;
		return true;
	}
	else if(touch.state == Touch::TouchState::Released)
	{
		if(touchID != touch.id)
		{
			return false;
		}
		else
		{
			touchID = 0;
			move = Vector2f(0.0,0.0);
			return true;
		}
	}
	else if(touch.state == Touch::TouchState::Move)
	{
		if(touchID != touch.id)
		{
			return false;
		}
		else
		{
			float dx = touch.x - touchX;
			float dy = touch.y - touchY;
			touchX = touch.x;
			touchY = touch.y;
			if(abs(dx) + abs(dy) < 4.0)
			{
				return true;
			}
			move = Vector2f::GetNomalize(dx, -dy);
			return true;
		}
	}
	return false;
}